import 'package:flutter/material.dart';

import 'bitmap.dart';
import 'canvas_view.dart';
import 'curtain_light_model.dart';
import 'list_type.dart';

class CurtainMatrixViewPage extends StatefulWidget {
  const CurtainMatrixViewPage({Key? key}) : super(key: key);

  @override
  State<CurtainMatrixViewPage> createState() => _CurtainMatrixViewPageState();
}

class _CurtainMatrixViewPageState extends State<CurtainMatrixViewPage> {
  late HSVColor selectColor;
  late CanvasType canvasType;
  late int displayX;
  late int displayY;
  late int amount;
  Bitmap? bitmap;
  late int? dynamicSpeed;
  int? dynamicType;

  @override
  void initState() {
    // TODO: implement initState
    selectColor = HSVColor.fromColor(Colors.red);
    canvasType = CanvasType.smear;
    amount = 1;
    DiyDeviceDirectionType directionType = DiyDeviceDirectionType.horizontal;
    CanvasData canvasData = getCanvasData(
      amount: amount,
      directionType: directionType,
    );
    displayX = canvasData.x;
    displayY = canvasData.y;
    Bitmap bitmap2 = canvasData.bitmap.clone();
    Color color = HSVColor.fromColor(Color(0xff303032)).withValue(0).toColor();
    bitmap2.pixels = List.generate(bitmap2.pixels.length, (index) => color);
    bitmap = bitmap2;
    super.initState();
  }

  CanvasData getCanvasData(
      {required int amount, required DiyDeviceDirectionType directionType}) {
    late Bitmap bitmap;
    int x = 20, y = 20;
    if (amount == 1) {
      bitmap = Bitmap.fillMyColor(x: x, y: y);
    } else {
      if (directionType == DiyDeviceDirectionType.horizontal) {
        x = x * amount;
        bitmap = Bitmap.fillMyColor(x: x, y: y);
      } else {
        y = y * amount;
        bitmap = Bitmap.fillMyColor(x: x, y: y);
      }
    }

    return CanvasData(x: x, y: y, bitmap: bitmap);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("CurtainMatrixView"),
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        color: Colors.white,
        child: Column(
          children: [
            Container(
              color: Colors.grey,
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.width,
              child: CurtainMatrixView(
                // color: selectColor,
                color: selectColor.toColor(),
                canvasType: canvasType,
                displayX: displayX,
                displayY: displayY,
                bitmap: bitmap!,
                amount: amount,
                directionType: DiyDeviceDirectionType.vertical,
                onChangedBitmap:(Bitmap bitmap){
                  // print("bitmap:${bitmap}");
                  // print("tag - step :${bitmap.step}");
                  //
                  // // dispatch(CurtainColorfulActionCreator.onSendColorFulCommand());
                  // dispatch(CurtainColorfulActionCreator.onSendMultiCommand());
                  // bitmap.clearStep();
                  // if(!isAll){
                  //
                  //   dispatch(CurtainColorfulActionCreator.onUpDateBitmap(bitmap));
                  // }
                  // dispatch(CurtainColorfulActionCreator.onFavoriteColor());
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
